package com.zhuiyun.educations.api.rotationimage.service.impl;

import cn.hutool.core.util.IdUtil;
import com.zhuiyun.educations.api.rotationimage.entity.ImageFile;
import com.zhuiyun.educations.api.rotationimage.entity.RotationImage;
import com.zhuiyun.educations.api.rotationimage.mapper.ImageFileMapper;
import com.zhuiyun.educations.api.rotationimage.mapper.RotationImageMapper;
import com.zhuiyun.educations.api.rotationimage.service.RotationImageService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.UploadFileUtils;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName RotationImageServiceImpl
 * @Description TODO 轮播图业务逻辑层实现类
 * @Author 杨友朋
 * @Date 2023/6/21 10:25
 **/
@Service
public class RotationImageServiceImpl implements RotationImageService {
    @Resource
    RotationImageMapper rotationImageMapper;

    @Resource
    ImageFileMapper imageFileMapper;

    @Resource
    UploadFileUtils uploadFileUtils;

    /**
     * @Author 杨友朋
     * @Description TODO 分页全查轮播图信息
     * @Date 2023/6/21 20:46
     * @Return PageResult
     * @param rotationImage，pageRequest
     */
    @Override
    public PageResult selectRotationImage(RotationImage rotationImage, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<RotationImage> list=rotationImageMapper.selectRotationImage(rotationImage);
        for (int i=0;i<list.size();i++){
            String imageIdStr = list.get(i).getImageId();
            System.out.println("++++++++++"+imageIdStr);
            if(imageIdStr.contains(",")){
                String[] imgId = imageIdStr.split(",");
                list.get(i).setImageUrl(imgId);
            }else {
                String[] imgId ={imageIdStr};
                list.get(i).setImageUrl(imgId);
            }

        }
        
        return MybatisPageHelper.getPageResult(list);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加单个图片
     * @Date 2023/6/21 20:46
     * @Return CommonResult
     * @param file，rotationImage，imageFile
     */
    @Override
    public CommonResult addRotationImage(MultipartFile file, RotationImage rotationImage, ImageFile imageFile) {
        //判断文件是否存在
        if (file!=null){
            //上传至服务器
            String s=uploadFileUtils.uploadFile(file);
            //判断图片id是否为空
            if(!StringUtils.isEmpty(s)){
                imageFile.setImageName(s);
            }
        }
        //判断实体类id是否为空
        if(rotationImage.getId()==null){
            imageFile.setCreateTime(new Date());
            imageFile.setUpdateTime(new Date());
            imageFile.setFileSize(file.getSize());
            imageFile.setFileUuidName(file.getOriginalFilename());
            imageFile.setFileType(file.getContentType());
            //持久化操作
            imageFileMapper.insertSelective(imageFile);

            rotationImage.setImageId(imageFile.getImageName());
            rotationImage.setCreateTime(new Date());
            rotationImage.setUpdateTime(new Date());
            //持久化操作
            rotationImageMapper.insertSelective(rotationImage);
            return CommonResult.ok("添加成功");
        }else {
            rotationImage.setImageId(imageFile.getImageName());
            rotationImage.setUpdateTime(new Date());
            //持久化操作
            rotationImageMapper.updateByPrimaryKeySelective(rotationImage);
            return CommonResult.ok("修改成功");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加一组轮播图
     * @Date 2023/6/21 20:46
     * @Return CommonResult
     * @param file，rotationImage，imageFile
     */
    @Override
    public CommonResult addRotationImages(MultipartFile[] file, RotationImage rotationImage, ImageFile imageFile) {
        //判断文件是否存在
        if (file!=null){
            //创建数组
            String[] imgId=new String[file.length];
            //循环将图片id存入数组
            for(int ids=0;ids<file.length;ids++){
                //上传至服务器 返回id
                String upl=uploadFileUtils.uploadFile(file[ids]);
                //判断图片id是否为空
                if(!StringUtils.isEmpty(upl)){
                    //将图片id存入数组
                    imgId[ids]=upl;
                    String s = file[ids].getOriginalFilename();
                    String type=s.substring(s.lastIndexOf("."),s.length());
                    imageFile.setImageName(upl);
                    imageFile.setCreateTime(new Date());
                    imageFile.setUpdateTime(new Date());
                    imageFile.setFileSize(file[ids].getSize());
                    imageFile.setFileUuidName(IdUtil.randomUUID()+"."+s);
                    imageFile.setFileType(type);
                    //持久化操作
                    imageFileMapper.insertSelective(imageFile);
                }else {
                    return CommonResult.error("添加失败");
                }
            }
            rotationImage.setImageId(StringUtils.join(imgId,','));
            rotationImage.setCreateTime(new Date());
            rotationImage.setUpdateTime(new Date());
            //持久化操作
            rotationImageMapper.insertSelective(rotationImage);
            return CommonResult.ok("添加成功");
        }else {
            return CommonResult.error("添加失败");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id删除轮播图信息
     * @Date 2023/6/21 20:46
     * @Return CommonResult
     * @param id
     */
    @Override
    public CommonResult deleteRotationImage(Integer id) {
        //判断id是否为空 不为空删除
        if (id!=null){
            //持久化操作
            rotationImageMapper.deleteRotationImage(id);
            return CommonResult.ok("删除成功");
        }else {
            return CommonResult.error("删除失败");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据ids批量删除轮播图信息
     * @Date 2023/6/21 20:47
     * @Return CommonResult
     * @param ids
     */
    @Override
    public CommonResult deleteImageByIds(List<Integer> ids) {
        //判断ids是否为空 不为空删除
        if (ids!=null){
            //持久化操作
            rotationImageMapper.deleteImageByIds(ids);
            return CommonResult.ok("删除成功");
        }else {
            return CommonResult.error("删除失败");
        }
    }
}
